package 测试连接_反射的应用一;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @Author: 小清远
 * @Time: 2023/7/3 20:36
 */
public class TestConnection {
    public static void main(String[] args) throws Exception{
//        String url = "jdbc:mysql://localhost:3306/testConnection?useSSL=false&serverTimezone=GMT";
        String url = "jdbc:mysql://localhost:3306/testConnection";
        String username = "root";
        String password = "password";
        String sql = "insert into stu(id,name) values(?,?)";
//        Class.forName("com.mysql.jdbc.Driver");

        Class.forName("com.mysql.cj.jdbc.Driver");
//        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testConnection?useSSL=false&serverTimezone=GMT", "root", "123");

        Connection con = DriverManager.getConnection(url, username, password);

        PreparedStatement ps = con.prepareStatement(sql);

        ps.setInt(1,8);
        ps.setString(2,"zl");

        int i = ps.executeUpdate();
        if(i==1){
            System.out.println("成功");
        }else{
            System.out.println("失败");
        }

        if(ps!=null){
            ps.close();
        }
        if(con!=null){
            con.close();
        }
    }
}
